home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / lib / Word.sig < prev    next >
Encoding:
Text File  |  1997-08-18  |  5.2 KB  |  142 lines  |  [TEXT/Moml]

  1. (* Word -- SML Basis Library *)
  2.  
  3. type word = word
  4. val wordSize : int
  5.  
  6. val orb  : word * word -> word
  7. val andb : word * word -> word
  8. val xorb : word * word -> word
  9. val notb : word -> word
  10.  
  11. val <<  : word * word -> word
  12. val >>  : word * word -> word
  13. val ~>> : word * word -> word
  14.  
  15. val +   : word * word -> word
  16. val -   : word * word -> word
  17. val *   : word * word -> word
  18. val div : word * word -> word
  19. val mod : word * word -> word
  20.  
  21. val >   : word * word -> bool
  22. val <   : word * word -> bool
  23. val >=  : word * word -> bool
  24. val <=  : word * word -> bool
  25. val compare : word * word -> order
  26.  
  27. val min : word * word -> word
  28. val max : word * word -> word
  29.  
  30. val toString   : word -> string
  31. val fromString : string -> word option
  32. val scan : StringCvt.radix 
  33.            -> (char, 'a) StringCvt.reader -> (word, 'a) StringCvt.reader
  34. val fmt  : StringCvt.radix -> word -> string
  35.  
  36. val toLargeWord   : word -> word
  37. val toLargeWordX  : word -> word    (* with sign extension *)
  38. val fromLargeWord : word -> word
  39.  
  40. val toLargeInt   : word -> int
  41. val toLargeIntX  : word -> int        (* with sign extension *)
  42. val fromLargeInt : int -> word
  43.  
  44. val toInt   : word -> int
  45. val toIntX  : word -> int        (* with sign extension *)
  46. val fromInt : int -> word
  47.  
  48.  
  49. (* [word] is the type of n-bit words, or n-bit unsigned integers.
  50.  
  51.    [wordSize] is the value of n above.  In Moscow ML, n=31 on 32-bit
  52.    machines and n=63 on 64-bit machines.
  53.  
  54.    [orb(w1, w2)] returns the bitwise `or' of w1 and w2.
  55.  
  56.    [andb(w1, w2)] returns the bitwise `and' of w1 and w2.
  57.  
  58.    [xorb(w1, w2)] returns the bitwise `exclusive or' or w1 and w2.
  59.  
  60.    [notb w] returns the bitwise negation of w.
  61.  
  62.    [<<(w, k)] returns the word resulting from shifting w left by k
  63.    bits.  The bits shifted in are zero, so this is a logical shift.
  64.    Consequently, the result is 0-bits when k >= wordSize.
  65.  
  66.    [>>(w, k)] returns the word resulting from shifting w right by k
  67.    bits.  The bits shifted in are zero, so this is a logical shift.
  68.    Consequently, the result is 0-bits when k >= wordSize.
  69.  
  70.    [~>>(w, k)] returns the word resulting from shifting w right by k
  71.    bits.  The bits shifted in are replications of the left-most bit:
  72.    the `sign bit', so this is an arithmetical shift.  Consequently,
  73.    for k >= wordSize and wordToInt w >= 0 the result is all 0-bits, and 
  74.    for k >= wordSize and wordToInt w <  0 the result is all 1-bits.
  75.  
  76.    To make <<, >>, and ~>> infix, use the declaration 
  77.                           infix 5 << >> ~>>
  78.  
  79.    [+, -, *, div, mod] represent unsigned integer addition,
  80.    subtraction, multiplication, division, and remainder, modulus two
  81.    to wordSize.  The operations (i div j) and (i mod j) raise Div when
  82.    j=0.  Otherwise no exceptions are raised.
  83.  
  84.    [w1 > w2] returns true if the unsigned integer represented by w1
  85.    is larger than that of w2, and similarly for <, >=, <=.  
  86.  
  87.    [compare(w1, w2)] returns LESS, EQUAL, or GREATER, according 
  88.    as w1 is less than, equal to, or greater than w2 (as unsigned integers).
  89.  
  90.    [min(w1, w2)] returns the smaller of w1 and w2 (as unsigned integers).
  91.  
  92.    [max(w1, w2)] returns the larger of w1 and w2 (as unsigned integers).
  93.  
  94.    [fmt radix w] returns a string representing w, in the radix (base)
  95.    specified by radix.
  96.  
  97.      radix    description                     output format  
  98.      ------------------------------------------------------  
  99.       BIN     unsigned binary      (base  2)  [01]+         
  100.       OCT     unsigned octal       (base  8)  [0-7]+          
  101.       DEC     unsigned decimal     (base 10)  [0-9]+          
  102.       HEX     unsigned hexadecimal (base 16)  [0-9A-F]+       
  103.  
  104.    [toString w] returns a string representing w in unsigned
  105.    hexadecimal format.  Equivalent to (fmt HEX w).
  106.    
  107.    [fromString s] returns SOME(w) if a hexadecimal unsigned numeral
  108.    can be scanned from a prefix of string s, ignoring any initial
  109.    whitespace; returns NONE otherwise.  Raises Overflow if the scanned 
  110.    number cannot be represented as a word.  An unsigned hexadecimal
  111.    numeral must have form, after possible initial whitespace:
  112.         [0-9a-fA-F]+
  113.  
  114.    [scan radix getc charsrc] attempts to scan an unsigned numeral from
  115.    the character source charsrc, using the accessor getc, and ignoring
  116.    any initial whitespace.  The radix argument specifies the base of
  117.    the numeral (BIN, OCT, DEC, HEX).  If successful, it returns
  118.    SOME(w, rest) where w is the value of the numeral scanned, and rest
  119.    is the unused part of the character source.  Raises Overflow if the
  120.    scanned number cannot be represented as a word.  A numeral must
  121.    have form, after possible initial whitespace:
  122.  
  123.      radix    input format 
  124.      -------------------------------------
  125.       BIN     (0w)?[0-1]+
  126.       OCT     (0w)?[0-7]+
  127.       DEC     (0w)?[0-9]+
  128.       HEX     (0wx|0wX|0x|0X)?[0-9a-fA-F]+
  129.  
  130.    [toInt w] returns the (signed) integer represented by bit-pattern w.
  131.    [toIntX w] returns the (signed) integer represented by bit-pattern w.
  132.    [fromInt i] returns the word representing integer i.
  133.  
  134.    [toLargeInt w] returns the (signed) integer represented by bit-pattern w.
  135.    [toLargeIntX w] returns the (signed) integer represented by bit-pattern w.
  136.    [fromLargeInt i] returns the word representing integer i.
  137.  
  138.    [toLargeWord w] returns w.
  139.    [toLargeWordX w] returns w.
  140.    [fromLargeWord w] returns w.
  141. *)
  142.